from model.group_model import GroupModel
from server import DBSession as db_session
# Dao 负责和数据库的交互
class GroupDao:

    def get(self, id: int) -> GroupModel:
        """
        根据id查询
        :param id: id
        :return: GroupModel
        """
        return db_session.query(GroupModel).filter_by(id=id).first()

    def get_by_name(self, name: str) -> GroupModel:
        """
        根据名称查询
        """
        return db_session.query(GroupModel).filter_by(name=name).first()

    def list(self):
        """
        返回所有
        :return:
        """
        return db_session.query(GroupModel).all()

    def create(self, model: GroupModel) -> int:
        """
        新增
        :param model: 对象
        :return:
        """
        db_session.add(model)
        db_session.commit()
        return model.id

    def delete(self, id: int) -> int:
        """
        删除
        :param id: id
        :return:
        """
        db_session.query(GroupModel).filter_by(id=id).delete()
        db_session.commit()
        return id

    def update(self, model: GroupModel) -> int:
        """
        更新
        :param model: model对象
        :param id: id
        :return:
        """
        db_session.query(GroupModel).filter_by(id=model.id).update(model.as_dict())
        db_session.commit()
        return model.id